package com.che.software.testato.web.controller;

import java.io.Serializable;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;

import org.apache.log4j.Logger;
import org.primefaces.context.RequestContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.che.software.testato.business.ServiceManager;
import com.che.software.testato.business.UserManager;
import com.che.software.testato.business.exception.ServiceCreationManagerException;
import com.che.software.testato.business.exception.ServiceSearchManagerException;
import com.che.software.testato.business.exception.UserSearchManagerException;
import com.che.software.testato.domain.entity.Service;
import com.che.software.testato.domain.entity.StatisticalService;
import com.che.software.testato.domain.entity.search.ServiceSearch;
import com.che.software.testato.web.controller.acontroller.AbstractController;

/**
 * Controller used to manage the services administration.
 * 
 * @author Clement HELIOU (clement.heliou@che-software.com).
 * @copyright Che Software.
 * @license GNU General Public License.
 * @see AbstractController, Serializable.
 * @since July, 2011.
 * 
 *        This file is part of Testato.
 * 
 *        Testato is free software: you can redistribute it and/or modify it
 *        under the terms of the GNU General Public License as published by the
 *        Free Software Foundation, either version 3 of the License, or (at your
 *        option) any later version.
 * 
 *        Testato is distributed in the hope that it will be useful, but WITHOUT
 *        ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *        FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 *        for more details.
 * 
 *        You should have received a copy of the GNU General Public License
 *        along with Testato. If not, see <http://www.gnu.org/licenses/>.
 * 
 *        Testato's logo is a creation of Arrioch
 *        (http://arrioch.deviantart.com/) and it's distributed under the terms
 *        of the Creative Commons License.
 */
@Component("servicesAdministrationController")
@ManagedBean(name = "servicesAdministrationController")
@Scope("session")
@SessionScoped
public class ServicesAdministrationController extends AbstractController implements Serializable {

	/**
	 * Constants.
	 */
	private static final Logger LOGGER = Logger.getLogger(ServicesAdministrationController.class);
	private static final long serialVersionUID = 6278475304590443498L;

	/**
	 * Members.
	 */
	private Service serviceToCreate;
	@Autowired
	private transient ServiceManager serviceManager;
	private List<StatisticalService> services;
	private StatisticalService[] selectedServices;
	@Autowired
	private transient UserManager userManager;

	/**
	 * Creating the object corresponding to the service to create.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @param ev the event launched during the creation of a service.
	 * @since July, 2011.
	 */
	public void createService(ActionEvent ev) {
		LOGGER.debug("createService(" + serviceToCreate.getLabel() + ").");
		try {
			serviceManager.createServiceFromLabel(serviceToCreate.getLabel());
			serviceToCreate = new Service();
			services = null; // To trigger the data loading.
			RequestContext.getCurrentInstance().addPartialUpdateTarget("servicesForm:servicesTable");
		} catch (ServiceCreationManagerException e) {
			LOGGER.error("Error during the creation of a service.", e);
			RequestContext.getCurrentInstance().addCallbackParam(NOT_VALID_CALLBACK_PARAM, true);
		}

	}

	/**
	 * Initializing the object corresponding to the service to create.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @param e the event launched during the creation of a service.
	 * @since July, 2011.
	 */
	public void initServiceToCreate(ActionEvent e) {
		LOGGER.debug("initServiceToCreate().");
		serviceToCreate = new Service();
	}

	/**
	 * Redirection to perform to get the services page.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @return the resulting redirection.
	 * @since August, 2011.
	 */
	public String onAccess() {
		LOGGER.debug("onAccess().");
		services = null; // To trigger the data loading.
		selectedServices = null;
		return "services.xhtml?faces-redirect=true";
	}

	/**
	 * Getter for the private field value serviceManager.
	 * 
	 * @return the serviceManager field value.
	 */
	public ServiceManager getServiceManager() {
		return serviceManager;
	}

	/**
	 * Setting a value to the serviceManager field.
	 * 
	 * @param serviceManager the value to set.
	 */
	public void setServiceManager(ServiceManager serviceManager) {
		this.serviceManager = serviceManager;
	}

	/**
	 * Getter for the private field value userManager.
	 * 
	 * @return the userManager field value.
	 */
	public UserManager getUserManager() {
		return userManager;
	}

	/**
	 * Setting a value to the userManager field.
	 * 
	 * @param userManager the value to set.
	 */
	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	/**
	 * Getter for the private field value services.
	 * 
	 * @return the services field value.
	 */
	public List<StatisticalService> getServices() {
		if (null == services) {
			LOGGER.debug("getServices():initialization.");
			try {
				services = serviceManager.searchStatisticalService(new ServiceSearch());
				if (null != services) {
					for (StatisticalService service : services) {
						service.setLastHiredUser(userManager.searchUserFromId(service.getUserId()));
					}
				}
			} catch (ServiceSearchManagerException e) {
				LOGGER.error("Error during the service search.", e);
			} catch (UserSearchManagerException e) {
				LOGGER.error("Error during the user search.", e);
			}
		}
		return services;
	}

	/**
	 * Setting a value to the services field.
	 * 
	 * @param services the value to set.
	 */
	public void setServices(List<StatisticalService> services) {
		this.services = services;
	}

	/**
	 * Getter for the private field value selectedServices.
	 * 
	 * @return the selectedServices field value.
	 */
	public StatisticalService[] getSelectedServices() {
		return selectedServices;
	}

	/**
	 * Setting a value to the selectedServices field.
	 * 
	 * @param selectedServices the value to set.
	 */
	public void setSelectedServices(StatisticalService[] selectedServices) {
		this.selectedServices = selectedServices;
	}

	/**
	 * Getter for the private field value serviceToCreate.
	 * 
	 * @return the serviceToCreate field value.
	 */
	public Service getServiceToCreate() {
		return serviceToCreate;
	}

	/**
	 * Setting a value to the serviceToCreate field.
	 * 
	 * @param serviceToCreate the value to set.
	 */
	public void setServiceToCreate(Service serviceToCreate) {
		this.serviceToCreate = serviceToCreate;
	}
}